//
//  LXBadgeBtn.m
//  微博
//
//  Created by imera on 15-9-13.
//  Copyright (c) 2015年 imera. All rights reserved.
//

#import "LXBadgeBtn.h"

@implementation LXBadgeBtn

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        //设置背景图片
        [self setBackgroundImage:[UIImage resizeWithName:@"main_badge"] forState:UIControlStateNormal];
        //设置提醒内容
        [self setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        //设置控件不可交互
        self.userInteractionEnabled = NO;
        //设置字体大小
        self.titleLabel.font = [UIFont systemFontOfSize:11];
        //隐藏控件
        self.hidden = YES;

    }
    return self;
}


- (void)setBadge:(NSString *)badge
{
    _badge = badge;
    
    if (badge) {
        self.hidden = NO;
        //设置badgeBtn的文字
        [self setTitle:self.badge forState:UIControlStateNormal];
    }
    
    //设置badgeButton的frame
    CGSize badgeSize = [self.badge sizeWithAttributes:@{NSFontAttributeName:self.titleLabel.font}];
    CGFloat badgeBtnX = 0;
    CGFloat badgeBtnY = 0;
    CGFloat badgeBtnW = self.currentBackgroundImage.size.width;
    //如果提醒数字大于1就拉伸
    if(self.badge.length > 1){
        badgeBtnW = badgeSize.width+10;
    };
    CGFloat badgeBtnH = self.currentBackgroundImage.size.height;
    
    self.frame = CGRectMake(badgeBtnX, badgeBtnY, badgeBtnW, badgeBtnH);

}

@end
